<template>
    <div class="addRoleDialog">
        <el-dialog :close-on-click-modal="isModelClose" :before-close="close" title="添加角色" :style="dialogStyle" v-if="roleDialogVisible" :visible.sync="roleDialogVisible">
            <el-form ref="roleForm" :model="form" :rules="rules">
                <el-form-item label="角色名称" :label-width="formLabelWidth" prop="name">
                    <el-input v-model="form.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="角色类型" :label-width="formLabelWidth" prop="roleType">
                    <el-input v-model="roleType" disabled autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="close">取 消</el-button>
                <el-button type="primary" @click="submitForm('roleForm')">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script lang="ts">
    import { Component, Emit, Prop, Vue } from 'vue-property-decorator'
    import AdminApiActions from '@/components/api/admin-api-actions'
    import $ from "jquery";
    @Component
    export default class AddRoleDialog extends Vue {
        @Prop() params: any;
        public paramData: any = {};
        public roleDialogVisible: boolean = true;
        public dialogStyle: any = {};
        public isModelClose: boolean = false;
        public formLabelWidth: any = "";
        public form: any = {
            username: '',
            phone: '',
            roleType: ""
        }
        //表单校验
        public rules: any = {
            username: [
                { required: true, message: '请输入角色名', trigger: 'blur' }
            ]
        }
        //创建时调用
        async created() {
            this.form = this.params.form;
            this.centerDialog();
        }
        public get roleType() {
            if (this.params.form.roleType === "1") {
                return "菜单角色";
            }
            return "操作角色";
        }
        @Emit("close")
        public close() {
            this.roleDialogVisible = false;
        }

        @Emit("confirm")
        public confirm() {
            this.roleDialogVisible = false;
        }
        //提交表单
        public submitForm(formName) {
            (this.$refs[formName] as any).validate((valid) => {
                if (valid) {
                    if (this.params.type == "add") {
                        this.saveRole()
                    } else {
                        this.updateRole();
                    }
                } else {
                    return false;
                }
            });
        }
        //保存分组
        public async saveRole() {
            await AdminApiActions.addRole(this.form)
            this.confirm();
        }
        //编辑分组
        public async updateRole() {
            await AdminApiActions.updateRole(this.form);
            this.confirm();
        }
        public centerDialog() {
            this.$nextTick(() => {
                let height = $(window).height();
                let dialogHeight = $(".addRoleDialog .el-dialog").height();
                this.dialogStyle = {
                    top: (height / 2 - dialogHeight / 2) + "px"
                }
            })
        }

    }
</script>
<style lang="less">
    .addRoleDialog {
        position: absolute;
        width: 100%;
        height: 100%;

        .el-dialog__body {
            padding-bottom: 0px;
        }

        .el-dialog {
            width: 40%;
            margin-top: 0px !important;

            .dialog-footer {
                display: flex;
                justify-content: center;
            }
        }
    }
</style>